package com.example.informationEntry.service.impl;

import com.example.informationEntry.dao.NaAdministratorUserDao;
import com.example.informationEntry.entity.NaAdministratorUser;
import com.example.informationEntry.service.NaAdministratorUserService;
import com.example.informationEntry.util.ResponseMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 管理员信息(NaAdministratorUser)表服务实现类
 *
 * @author makejava
 * @since 2019-04-08 11:20:33
 */
@Service("naAdministratorUserService")
public class NaAdministratorUserServiceImpl implements NaAdministratorUserService {
    @Resource
    private NaAdministratorUserDao naAdministratorUserDao;


    private static final Logger LOG = LoggerFactory.getLogger(NaAdministratorUserServiceImpl.class);


    /**
     * 通过ID查询单条数据
     *
     * @param naAdministratorUser 实例对象
     * @return 实例对象
     */
    @Override
    public NaAdministratorUser queryById(NaAdministratorUser naAdministratorUser) {
        return this.naAdministratorUserDao.queryById(naAdministratorUser);
    }

    /**
     * 查询多条数据
     *
     * @param naAdministratorUser 实例对象
     * @return list集合
     */
    @Override
    public List<NaAdministratorUser> queryAll(NaAdministratorUser naAdministratorUser) {
        return this.naAdministratorUserDao.queryAll(naAdministratorUser);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<NaAdministratorUser> queryAllByLimit(int offset, int limit) {
        return this.naAdministratorUserDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param naAdministratorUser 实例对象
     * @return 实例对象
     */
    @Override
    public Map<String, Object> insert(NaAdministratorUser naAdministratorUser) {
        try {
            if (this.queryById(naAdministratorUser) != null) {
                return ResponseMessage.isException("用户名已存在");
            }
            naAdministratorUserDao.insert(naAdministratorUser);
        } catch (DuplicateKeyException e) {
            LOG.error("数据库链接异常：" + e);
            return ResponseMessage.isException("用户名已存在");
        } catch (Exception e) {
            LOG.error("数据库链接异常：" + e);
            return ResponseMessage.isException("出现错误,如数据库链接池已修改，请联系小白，异常为：" + e.getMessage());
        }
        return ResponseMessage.isSuccess("管理员添加成功。");
    }

    /**
     * 修改数据
     *
     * @param naAdministratorUser 实例对象
     * @return 实例对象
     */
    @Override
    public NaAdministratorUser update(NaAdministratorUser naAdministratorUser) {
        this.naAdministratorUserDao.update(naAdministratorUser);
        return this.queryById(naAdministratorUser);
    }

    /**
     * 通过主键删除数据
     *
     * @param userName 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String userName) {
        return this.naAdministratorUserDao.deleteById(userName) > 0;
    }

    /**
     * 进行数据规整为 k:用户账号  V：用户姓名
     *
     * @return 规整后的参数
     */
    @Override
    public Map<String, String> queryUserMap() {
        List<NaAdministratorUser> adminList = this.naAdministratorUserDao.queryAll(new NaAdministratorUser());
        Map<String, String> adminMap = new HashMap<>();
        for (NaAdministratorUser adminDatum : adminList) {
            adminMap.put(adminDatum.getUserPhone(), adminDatum.getUserName());
        }
        return adminMap;
    }


}